package com.cskaoyan.javase.homework.day13.stage2.dao.impl;


import com.cskaoyan.javase.homework.day13.stage2.dao.StudentDao;
import com.cskaoyan.javase.homework.day13.stage2.model.Student;
import com.cskaoyan.javase.homework.day13.stage2.model.StudentData;

/**
 * 与学生Student相关的，所有数据处理都在该类下进行
 *
 * @since 14:26
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class StudentDaoImpl implements StudentDao {

    // 从数据源获取数据
    private Student[] STUDS = StudentData.STUDS;
    private String[] COLUMNS = StudentData.COLUMNS;

    @Override
    public String[] getTableColumns() {
        return COLUMNS;
    }

    /**
     * 该方法返回Student对象数组中,真实存在的学生对象
     * @since 22:10
     * @return com.cskaoyan.javase.homework.day13.stage2.model.Student[]
     * @author wuguidong@cskaoyan.onaliyun.com
     */
    @Override
    public Student[] getAllRealStudent() {
        int count = 0;
        for (Student stud : STUDS) {
            if (stud != null) {
                count++;
            }
        }
        Student[] result = new Student[count];
        int newIndex = 0;
        for (Student stud : STUDS) {
            if (stud != null) {
                result[newIndex] = stud;
                newIndex++;
            }
        }
        return result;
    }

    /**
     * 根据传入的id,校验,重复返回true,不重复返回false
     * @since 22:23
     * @param id 需要校验的id
     * @return boolean
     * @author wuguidong@cskaoyan.onaliyun.com
     */
    @Override
    public boolean checkStuIdRepeat(String id) {
        // 变量真实对象数组
        for (Student stu : getAllRealStudent()) {
            if (stu.getStuId().equals(id)){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据传入的学生对象,插入.成功返回true,否者返回false
     * @since 22:29
     * @param stu
     * @return boolean
     * @author wuguidong@cskaoyan.onaliyun.com
     */
    @Override
    public boolean createStudent(Student stu) {
        for (int i = 0; i < STUDS.length; i++) {
            if (STUDS[i] == null){
                STUDS[i] = stu;
                return true;
            }
        }
        return false;
    }

}
